package com.xblibo.xruth.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.mlw.vlh.ValueList;

import org.apache.commons.beanutils.DynaBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.xblibo.xruth.common.BaseController;
import com.xblibo.xruth.common.CommonEnum.NewOrOld;
import com.xblibo.xruth.entity.Shopper;
import com.xblibo.xruth.entity.User;
import com.xblibo.xruth.service.ShopperService;
import com.xblibo.xruth.service.UserService;

@Controller
public class ShopperAction extends BaseController{
	
	@Resource
	private ShopperService shopperService;
	@Resource
	private UserService userService;
	
	public Boolean isLogin(HttpServletRequest request){
		User user = getCurrentUser();
		return user != null;
	}

	@RequestMapping("/shopper/index")
	public String list(HttpServletRequest request,Model model){
		List<Shopper> shoppers = new ArrayList<Shopper>();//shopperService.findCurrentDayShopper(user.getCompanyId());
		Integer allEnterCount = 0;
		Integer allTryonCount = 0;
		Integer allTradeCount = 0;
		Double allTradeAmount = 0.0;
		
		Integer allNewEnterCount = 0;
		Integer allNewTryonCount = 0;
		Integer allNewTradeCount = 0;
		Double allNewTradeAmount = 0.0;
		
		
		Integer allOldEnterCount = 0;
		Integer allOldTryonCount = 0;
		Integer allOldTradeCount = 0;
		Double allOldTradeAmount = 0.0;
		
		for(Shopper shopper:shoppers){
			allEnterCount  += shopper.getEnterCount();
			allTryonCount  += shopper.getTryonCount();
			allTradeCount  += shopper.getTradeCount();
			allTradeAmount += shopper.getTradeAmount();
			if(NewOrOld.新顾客.equals(shopper.getNewOrOld())){
				allNewEnterCount += shopper.getEnterCount();
				allNewTryonCount  += shopper.getTryonCount();
				allNewTradeCount  += shopper.getTradeCount();
				allNewTradeAmount += shopper.getTradeAmount();
			}else{
				allOldEnterCount += shopper.getEnterCount();
				allOldTryonCount  += shopper.getTryonCount();
				allOldTradeCount  += shopper.getTradeCount();
				allOldTradeAmount += shopper.getTradeAmount();
			}
		}
		
		
		
		//今日进店所有顾客汇总
		model.addAttribute("allEnterCount", allEnterCount);
		model.addAttribute("allTryonCount", allTryonCount);
		model.addAttribute("allTradeCount", allTradeCount);
		model.addAttribute("allTradeAmount", allTradeAmount);
		
		if(allEnterCount ==0){
			model.addAttribute("tryonPre", 0);
			model.addAttribute("tradePre", 0);
			model.addAttribute("amountPre", 0);
		}else{
			java.math.BigDecimal deSource = new java.math.BigDecimal((allTryonCount*100.0/allEnterCount)+""); 
			Double tryonPre = Double.valueOf(deSource.setScale(2,java.math.BigDecimal.ROUND_DOWN).doubleValue());
			
			
			java.math.BigDecimal deSource1 = new java.math.BigDecimal((allTradeCount*100.0/allEnterCount)+""); 
			Double tradePre = Double.valueOf(deSource1.setScale(2,java.math.BigDecimal.ROUND_DOWN).doubleValue());
			
			java.math.BigDecimal deSource2 = new java.math.BigDecimal((allTradeAmount*100.0/allEnterCount)+""); 
			Double amountPre = Double.valueOf(deSource2.setScale(2,java.math.BigDecimal.ROUND_DOWN).doubleValue());

			model.addAttribute("tryonPre", tryonPre);
			model.addAttribute("tradePre", tradePre);
			model.addAttribute("amountPre", amountPre);
		}
		
		//今日进店新顾客汇总
		model.addAttribute("allNewEnterCount", allNewEnterCount);
		model.addAttribute("allNewTryonCount", allNewTryonCount);
		model.addAttribute("allNewTradeCount", allNewTradeCount);
		model.addAttribute("allNewTradeAmount", allNewTradeAmount);
		
		if(allNewEnterCount ==0){
			model.addAttribute("tryonNewPre", 0);
			model.addAttribute("tradeNewPre", 0);
			model.addAttribute("amountNewPre", 0);
		}else{
			model.addAttribute("tryonNewPre", allNewTryonCount/allNewEnterCount);
			model.addAttribute("tradeNewPre", allNewTradeCount/allNewEnterCount);
			model.addAttribute("amountNewPre", allNewTradeAmount/allNewEnterCount);
		}
		
		//今日进店老顾客汇总
		model.addAttribute("allOldEnterCount", allOldEnterCount);
		model.addAttribute("allOldTryonCount", allOldTryonCount);
		model.addAttribute("allOldTradeCount", allOldTradeCount);
		model.addAttribute("allOldTradeAmount", allOldTradeAmount);
		
		if(allOldEnterCount ==0){
			model.addAttribute("tryonOldPre", 0);
			model.addAttribute("tradeOldPre", 0);
			model.addAttribute("amountOldPre", 0);
		}else{
			model.addAttribute("tryonOldPre", allOldTryonCount/allOldEnterCount);
			model.addAttribute("tradeOldPre", allOldTradeCount/allOldEnterCount);
			model.addAttribute("amountOldPre", allOldTradeAmount/allOldEnterCount);
		}
		
		model.addAttribute("shoppers",shoppers);
		return "/shopper/shopper_list";
	}

	@RequestMapping(value = "/shopper/add1")
	public String addForm(HttpServletRequest request){
		if(!isLogin(request)){
			return "user_login";
		}
		List<User> users = userService.findBy("companyId", getCurrentUser().getCompanyId());
		request.setAttribute("users",users);
		request.setAttribute("today",getToday());
		return "/shopper/shopper_add";
	}

	

	@RequestMapping(value = "/shopper/save", method = RequestMethod.POST)
	public String save(Shopper shopper,HttpServletRequest request)	{
		if(!isLogin(request)){
			return "user_login";
		}
		shopper.setCompanyId(getCurrentUser().getCompanyId());
		
		shopperService.saveOrUpdate(shopper);

		return "redirect:/shopper/list";//重定向
	}

	

	/**
	 * 跳转到报表页面
	 */
	@RequestMapping(value = "/shopper/report")
	public String report(){
		return "/shopper/shopper_report";//重定向
	}
	
	/**
	 * 进店查询
	 */
	@RequestMapping(value = "/shopper/query")
	public String query1(HttpServletRequest request,Model model){
		params.put("companyId",getCurrentUser().getCompanyId());
		
		ValueList   valueList  =  getValueList("shopper_list", params,request);
		request.setAttribute("valueList", valueList);
		
		List<User> users = userService.findBy("compayId", getCurrentUser().getCompanyId());
		request.setAttribute("users",users);
		
		int enter_count=0,tryon_count=0,trade_count=0,trade_amount=0;
		
		List<DynaBean> list = valueList.getList();
		for(DynaBean bean: list){
			enter_count  += Integer.parseInt(bean.get("enter_count").toString());
			tryon_count  += Integer.parseInt(bean.get("tryon_count").toString());
			trade_count	 += Integer.parseInt(bean.get("trade_count").toString());
			trade_amount += Double.parseDouble(bean.get("trade_amount").toString());
		}
		
		
		//查询的进店所有顾客汇总
		model.addAttribute("allEnterCount", enter_count);
		model.addAttribute("allTryonCount", tryon_count);
		model.addAttribute("allTradeCount", trade_count);
		model.addAttribute("allTradeAmount", trade_amount);
		
		if(enter_count ==0){
			model.addAttribute("tryonPre", 0);
			model.addAttribute("tradePre", 0);
			model.addAttribute("amountPre", 0);
		}else{
			java.math.BigDecimal deSource = new java.math.BigDecimal((tryon_count*100.0/enter_count)+""); 
			Double tryonPre = Double.valueOf(deSource.setScale(2,java.math.BigDecimal.ROUND_DOWN).doubleValue());
			
			java.math.BigDecimal deSource1 = new java.math.BigDecimal((trade_count*100.0/enter_count)+""); 
			Double tradePre = Double.valueOf(deSource1.setScale(2,java.math.BigDecimal.ROUND_DOWN).doubleValue());
			
			java.math.BigDecimal deSource2 = new java.math.BigDecimal(( trade_amount*100.0/enter_count)+""); 
			Double amountPre = Double.valueOf(deSource2.setScale(2,java.math.BigDecimal.ROUND_DOWN).doubleValue());
			
			model.addAttribute("tryonPre", tryonPre);
			model.addAttribute("tradePre", tradePre);
			model.addAttribute("amountPre", amountPre);
		}
		
		
		return "/shopper/shopper_query";//重定向
	}
	
	/**
	 * 周报表
	 */
	@RequestMapping(value="/shopper/weekreport")
	public String weekreport(String startDate,String endDate,Model model,HttpServletRequest request){
		System.out.println("startDate:"+startDate+"   endDate:"+endDate);
		
		ValueList   valueList  =  getValueList("shopper_list", params,request);
		request.setAttribute("valueList", valueList);
		
		
		
		return "/shopper/shopper_report";
	}
}
